﻿using System;
using System.Configuration;
using System.IO;
using System.Web.Mvc;
using System.Web.UI;
using Recaptcha;

namespace CodeContrib.Web.Mvc.Html
{
	public static class CaptchaExtensions
	{
		public static MvcHtmlString Captcha(this HtmlHelper htmlHelper)
		{
			return Captcha(htmlHelper, CaptchaTheme.Red);
		}

		public static MvcHtmlString Captcha(this HtmlHelper htmlHelper, CaptchaTheme theme)
		{
			var captcha = new RecaptchaControl
				              {
					              ID = "recaptcha",
					              PrivateKey = Convert.ToString(ConfigurationManager.AppSettings["ReCaptchaPrivateKey"]),
					              PublicKey = Convert.ToString(ConfigurationManager.AppSettings["ReCaptchaPublicKey"]),
					              Theme = theme.ToStringLower()
				              };

			var htmlWriter = new HtmlTextWriter(new StringWriter());

			captcha.RenderControl(htmlWriter);

			return MvcHtmlString.Create(htmlWriter.InnerWriter.ToString());
		}
	}
}